package javanio20;

import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

public class SelectorDemo
{

	public static void main(String[] args) throws Exception
	{
		int ports[] = {8000,8001,8003,8005,8006};
		//通过open()方法找到selector
		Selector selector = Selector.open();
		for(int i=0;i<ports.length;i++)
		{
			ServerSocketChannel initSer = null;
			//打开服务器的通道
			initSer = ServerSocketChannel.open();
			//配置服务器为非阻塞
			initSer.configureBlocking(false);
			ServerSocket initSock = initSer.socket();
			InetSocketAddress address = null;
			//实例化绑定地址
			address = new InetSocketAddress(ports[i]);
			//进行服务的绑定
			initSock.bind(address);
			initSer.register(selector, SelectionKey.OP_ACCEPT);
			System.out.println("服务器运行，在"+ports[i]+"端口监听。");
		}
	}

}
